package cn.itcast.erp.controller;

import cn.itcast.erp.annotation.CheckPower;
import cn.itcast.erp.entity.Emp;
import cn.itcast.erp.entity.utils.AjaxResult;
import cn.itcast.erp.entity.utils.ResultAjax;
import cn.itcast.erp.exception.SameUsernameException;
import cn.itcast.erp.service.EmpService;
import org.apache.commons.io.IOUtils;
import org.springframework.format.datetime.DateFormatter;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/emp")
public class EmpController {

    @Resource(name = "empService")
    private EmpService empService;


    @CheckPower("员工")
    @RequestMapping(value = "/insert",method = RequestMethod.POST)
    public Map insertEmp(@Validated(Emp.interGroup.class) Emp emp){
        this.empService.insertEmp (emp);
        return Collections.singletonMap ("message","添加成功");
    }

    @CheckPower("员工")
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public Map deleteEmp(Long uuid){
        this.empService.deleteEmp (uuid);
        return Collections.singletonMap ("message","删除成功");
    }


    // TODO: 2019/6/14 返回结果类型要统一, 不是已经有AjaxResult对象来返回对应格式数据了么
    @CheckPower("员工")
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Map deleteEmp(@Validated(Emp.interGroup.class) Emp emp){
        this.empService.updateEmp (emp);
        return Collections.singletonMap ("message","更新成功");
    }

    @CheckPower("重置密码")
    @RequestMapping(value = "/find",method = RequestMethod.POST)
    public AjaxResult searchEmp(Emp param){
        return this.empService.searchEmp (param);
    }


    @CheckPower("用户角色设置")
    @RequestMapping("/findAll")
    public List<Emp> searchEmpAll(Emp param){
        return this.empService.searchEmpAll(param);
    }

    @CheckPower("重置密码")
    @RequestMapping("/updatePwd/reset")
    public ResultAjax updatePwd(String id, String newPwd){
        ResultAjax resultAjax = new ResultAjax();
        int i = empService.updatePwd(id, newPwd);
        if (i > 0){
            resultAjax.setCode(1);
            resultAjax.setMessage("修改成功!!");
            return resultAjax;
        }else {
            resultAjax.setCode(0);
            resultAjax.setMessage("修改失败!!");
            return resultAjax;
        }

    }

    //修改密码
    @CheckPower("员工")
    @RequestMapping("/updatePwd")
    public AjaxResult updatePwd(@SessionAttribute("emp") Emp emp,
                                @Validated(Emp.updatePwd.class)Emp emps){

        AjaxResult ar = this.empService.updatePwd(emp.getUuid(), emps.getPwd(), emps.getNewPwd());

        return ar;
    }

    @CheckPower("重置密码")
    @RequestMapping("/findAllEmp")
    public List<Emp> findAllEmp(){
      return this.empService.findAllEmp();
    }


    @CheckPower("员工")
    @RequestMapping("/findById")
    public Emp findEmpById(Long uuid){
        return this.empService.searchEmpById (uuid);
    }



    //时间转换器
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.addCustomFormatter(new DateFormatter("yyyy-MM-dd"));
    }


    //导出Excel
    @CheckPower("员工")
    @RequestMapping("/export")
    public void getAllEmpEx(HttpServletResponse response) {
        try {
            response.reset();
            response.setContentType("application/msexcel;charset=utf-8");
            response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("员工信息.xls", "UTF-8"));
            InputStream inputStream = empService.getAllEmpEx();
            IOUtils.copy(inputStream, response.getOutputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


}
